<?php

require_once dirname(__FILE__).'/../lib/SettingGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/SettingGeneratorHelper.class.php';

/**
 * Setting actions.
 *
 * @package    billingSystem
 * @subpackage Setting
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class SettingActions extends autoSettingActions
{
    public function executeIndex(sfWebRequest $request)
    {
        $this->vendor=new Setting();
        $vendorArray=$this->vendor->searchVendor();
        $this->vendor=$vendorArray[0];
        $this->flag=true;
    }
  public function executeShow(sfWebRequest $request)
  {
    $files=Array();
    $i=0;
    $dir_ptr = opendir("/opt/lampp/htdocs/billingSystem/csv");
    while ($file = readdir($dir_ptr))
    {
        if ($file != "." && $file != ".." && $file!=".svn")
        {
             $files[$i]=$file;
        }

    }
    closedir($dir_ptr);
    foreach($files as $file)
    {
        $transFile=fopen("/opt/lampp/htdocs/billingSystem/csv/".$file,"r");
        if($transFile!=false)
        {
                $fields=Array("transid","custfname","custlname","pid","proid","paidtransfees","date");
                while(!feof($transFile))
                {
                    $line=fgets($transFile);
                    if($line!=null)
                    {
                        $arr=explode(";",$line);
                        $transaction=new Transactions();
                        $i=0;
                        foreach($arr as $val)
                        {
                            $transaction->set($fields[$i],$val);
                            $i++;
                        }
                        $transaction->save();
                    }
                }
                unlink("/opt/lampp/htdocs/billingSystem/csv/".$file);
        }
    }
    $partner=new Partner();
    $transaction=new Transactions();
    $this->transactionsArray=$transaction->filterTransactions();
    foreach($this->transactionsArray as $trans)
    {
        $part=$partner->searchPartner($trans->get('pid'));
        if($part[0]->get('distype')=="Fixed")
        {
           $money=$trans->get('sum')-($part[0]->get('disvalue')*$trans->get('count'));
        }
        else
        {
           $money=$trans->get('sum')-($part[0]->get('disvalue')*$trans->get('sum')/100);
        }
       $invoice=new Invoice();
       $invoice->setCrdate(date('Y-m-d',time()));
       $invoice->setTotmoney($money);
       $invoice->setPid($trans->get('pid'));
       $invoice->setPaystatus('none');
       $invoice->save();
       $invid=$invoice->getInvid();
       $productArray= $transaction->productsTransactions($trans->get('pid'));
       foreach($productArray as $prod)
       {
           $invoicedetails=new Invoicedetails();
           $invoicedetails->setInvid($invid);
           $invoicedetails->setProid($prod->get('proid'));
           $invoicedetails->setTotnumpro($prod->get('count'));
           $invoicedetails->setTotalmoneyproduct($prod->get('sum'));
           $invoicedetails->save();
       }
       $transArray=$transaction->searchTransactions($trans->get('pid'));
       foreach($transArray as $transObject)
       {
           $transObject->setInvid($invid);
           $transObject->save();
       }
    }
    $this->vendor=$this->getRoute()->getObject();
    $this->flag=false;
    $this->setTemplate('index');
  }
}
